package com.jandar.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cl.mvc.service.BaseService;
import com.jandar.entity.ExcelPerson;
import com.jandar.entity.ExcelPersonInfo;
import com.jandar.entity.ExcelPersonRecord;
import com.jandar.repository.ExcelPersonRepository;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

import static com.jandar.constant.QTableConstant.*;

@Service
public class ExcelPersonService extends BaseService<ExcelPerson> {
    private final JPAQueryFactory queryFactory;

    protected ExcelPersonService(ExcelPersonRepository ExcelPersonRepository, JPAQueryFactory queryFactory) {
        super(ExcelPersonRepository);
        this.queryFactory = queryFactory;
    }

    public List<Map<String, Object>> countDetail(Integer personId, String queryField) {

        if ("phoneCount".equals(queryField)) {
            List<ExcelPersonInfo> phoneList = queryFactory.select(qExcelPersonInfo).from(qExcelPersonInfo).where(qExcelPersonInfo.personId.eq(personId).and(qExcelPersonInfo.type.contains("手机"))).fetch();
            List<Map<String, Object>> res = phoneList.stream().map(item -> {
                Map<String, Object> map = new TreeMap<>();
                map.put("手机号", item.getNumber());
                return map;
            }).collect(Collectors.toList());
            return res;
        } else if ("cardCount".equals(queryField)) {
            List<ExcelPersonInfo> cardList = queryFactory.select(qExcelPersonInfo).from(qExcelPersonInfo).where(qExcelPersonInfo.personId.eq(personId).and(qExcelPersonInfo.type.contains("银行"))).fetch();
            List<Map<String, Object>> res = cardList.stream().map(item -> {
                Map<String, Object> map = new TreeMap<>();
                map.put("银行卡号", item.getNumber());
                return map;
            }).collect(Collectors.toList());
            return res;
        } else if ("billCount".equals(queryField)) {
            int billTypeParentId = queryFactory.select(qExcelSourceType.id).from(qExcelSourceType).where(qExcelSourceType.name.contains("手机话单").and(qExcelSourceType.parentId.isNull())).fetchFirst();
            List<Integer> billTypeId = queryFactory.select(qExcelSourceType.id).from(qExcelSourceType).where(qExcelSourceType.parentId.eq(billTypeParentId)).fetch();
            List<ExcelPersonRecord> billRecordList = queryFactory.select(qExcelPersonRecord).from(qExcelPersonRecord).leftJoin(qExcelSourceRecord).on(qExcelPersonRecord.recordId.eq(qExcelSourceRecord.id)).where(qExcelPersonRecord.personId.eq(personId).and(qExcelSourceRecord.typeId.in(billTypeId))).fetch();
            List<Map<String, Object>> res = billRecordList.stream().map(item -> {
                Map<String, Object> map = new TreeMap<>();
                map.put("文件名称", item.getExcelSourceRecord().getName());
                map.put("类型", item.getExcelSourceRecord().getTypeName());
                map.put("状态", item.getExcelSourceRecord().getStatus());
                map.put("创建时间", item.getExcelSourceRecord().getCreateTime());
                return map;
            }).collect(Collectors.toList());
            return res;
        } else if ("bankCount".equals(queryField)) {
            int bankTypeParentId = queryFactory.select(qExcelSourceType.id).from(qExcelSourceType).where(qExcelSourceType.name.contains("银行账单").and(qExcelSourceType.parentId.isNull())).fetchFirst();
            List<Integer> bankTypeId = queryFactory.select(qExcelSourceType.id).from(qExcelSourceType).where(qExcelSourceType.parentId.eq(bankTypeParentId)).fetch();
            List<ExcelPersonRecord> bankRecordList = queryFactory.select(qExcelPersonRecord).from(qExcelPersonRecord).leftJoin(qExcelSourceRecord).on(qExcelPersonRecord.recordId.eq(qExcelSourceRecord.id)).where(qExcelPersonRecord.personId.eq(personId).and(qExcelSourceRecord.typeId.in(bankTypeId))).fetch();
            List<Map<String, Object>> res = bankRecordList.stream().map(item -> {
                Map<String, Object> map = new TreeMap<>();
                map.put("文件名称", item.getExcelSourceRecord().getName());
                map.put("类型", item.getExcelSourceRecord().getTypeName());
                map.put("状态", item.getExcelSourceRecord().getStatus());
                map.put("创建时间", item.getExcelSourceRecord().getCreateTime());
                return map;
            }).collect(Collectors.toList());
            return res;
        }
        throw new RuntimeException("queryField参数错误");
    }

    public Page<ExcelPerson> page(ExcelPerson excelPerson, String auth) {
        Page<ExcelPerson> page = this.repository.findAll(this.buildSpecification(excelPerson).and(this.buildPowerSpecification(auth)), PageRequest.of(excelPerson.getPageNumber() - 1, excelPerson.getPageSize()));
        int billTypeParentId = queryFactory.select(qExcelSourceType.id).from(qExcelSourceType).where(qExcelSourceType.name.contains("手机话单").and(qExcelSourceType.parentId.isNull())).fetchFirst();
        int bankTypeParentId = queryFactory.select(qExcelSourceType.id).from(qExcelSourceType).where(qExcelSourceType.name.contains("银行账单").and(qExcelSourceType.parentId.isNull())).fetchFirst();
        List<Integer> billTypeId = queryFactory.select(qExcelSourceType.id).from(qExcelSourceType).where(qExcelSourceType.parentId.eq(billTypeParentId)).fetch();
        List<Integer> bankTypeId = queryFactory.select(qExcelSourceType.id).from(qExcelSourceType).where(qExcelSourceType.parentId.eq(bankTypeParentId)).fetch();
        for (ExcelPerson person : page.getContent()) {
            Integer personId = person.getId();
            Long phoneCount = queryFactory.select(qExcelPersonInfo.count()).from(qExcelPersonInfo).where(qExcelPersonInfo.personId.eq(personId).and(qExcelPersonInfo.type.contains("手机"))).fetchFirst();
            Long cardCount = queryFactory.select(qExcelPersonInfo.count()).from(qExcelPersonInfo).where(qExcelPersonInfo.personId.eq(personId).and(qExcelPersonInfo.type.contains("银行"))).fetchFirst();
            Long billCount = queryFactory.select(qExcelPersonRecord.count()).from(qExcelPersonRecord).leftJoin(qExcelSourceRecord).on(qExcelPersonRecord.recordId.eq(qExcelSourceRecord.id)).where(qExcelPersonRecord.personId.eq(personId).and(qExcelSourceRecord.typeId.in(billTypeId))).fetchFirst();
            Long bankCount = queryFactory.select(qExcelPersonRecord.count()).from(qExcelPersonRecord).leftJoin(qExcelSourceRecord).on(qExcelPersonRecord.recordId.eq(qExcelSourceRecord.id)).where(qExcelPersonRecord.personId.eq(personId).and(qExcelSourceRecord.typeId.in(bankTypeId))).fetchFirst();

            Map<String, Object> statistics = new HashMap<>();
            statistics.put("phoneCount", phoneCount);
            statistics.put("cardCount", cardCount);
            statistics.put("billCount", billCount);
            statistics.put("bankCount", bankCount);
            person.setStatistics(statistics);
        }
        return page;
    }
}
